//
// Created by andrew on 2022/5/12.
//

#include <iostream>
#include <string>
#include <iostream>
#include <gtest/gtest.h>




using namespace std;
class Solution {
public:
    static string longestCommonPrefix(vector<string>& strs) {
        int size = strs.size();
        if (size == 0) return "";
        if (size == 1) return strs[0];

        auto subStr = strs[0];
        for (int i = 1; i < size; i ++) {
            subStr = getSubstring(subStr, strs[i]);
        }

        return subStr;
    }

    static string getSubstring(string& s1, string& s2) {
        int i = 0;
        if (s1.empty() || s2.empty()) {
            return "";
        }
        int length = std::min(s1.size(), s2.size());

        while(i < length && s1[i] == s2[i]) {
            i ++;
        }
        return s1.substr(0, i);
    }
};


class Solution1 {
public:
    string longestCommonPrefix(vector<string>& strs) {
        if (strs.empty()) return "";
        string prefix = strs[0];
        for (int i = 1; i < strs.size(); ++i) {
            while (strs[i].find(prefix) != 0) {
                prefix = prefix.substr(0, prefix.length() - 1);
                if (prefix.empty()) return "";
            }
        }
        return prefix;
    }
};



TEST(LongestCommonPrefixTest, longestCommonPrefix) {

    std::vector<string> strs = {"C", "C"};
    EXPECT_STREQ("da" , Solution::longestCommonPrefix(strs).c_str());

}

